<template>
  <div>
    <!-- 最终使用版：商品点单列表 -->
    <!-- 商品点单页头部 -->
    <header-top>
      <div class="left" slot="left">
        <router-link to="/">
          <i class="iconfont icon-zuo"></i>
          <span>返回</span>
        </router-link>
      </div>

      <div class="right" slot="right">
        <router-link to="/store">
          <!-- <span>{{addressTitle}}</span> -->
          <span v-if="typeof this.addressInfoNow == 'object'">{{addressInfoNow.title}}</span>
          <span v-else>{{addressStore.title}}</span>
          <img src="./imgs/ditu.svg" height="30px" width="30px"/>
        </router-link>
      </div>
    </header-top>

    <!-- 搜索框 -->
    <div class="search-wrap">
      <div class="search">
        <div class="search-icon"></div>
        <input type="text" value="" v-model="value" placeholder="有机青菜粥" />
      </div>
    </div>

    <!-- 商品内容部分 -->
    <!-- 商品类别 -->
    <div class="content" @click="isPosition">
      <div class="left">
        <ul>
          <li
            v-for="(item, i) of categoryName"
            :key="i"
            @click="changeCategory(item.scId)"
            :class="{ click: categoryId == i + 1 }"
          >
            <img :src="`/imgs/categoryName${item.scId}.svg`" alt="" />
            <p>{{ item.categoryName }}</p>
          </li>
        </ul>
      </div>

      <!-- 商品列表 -->
      <div class="right">
        <ul>
          <product-item v-for="(item, i) of shopList" :key="i" :item="item" :i="i">
          </product-item>
        </ul>
      </div>
    </div>

    <!-- 商品详情 -->

    <!-- 购物车子组件 -->
    <shop-cart></shop-cart>

    
  </div>
</template>


<script>
// 引入vuex
import { mapState } from "vuex";
import { Dialog } from "vant";
import { MessageBox } from "mint-ui";

// 引入头部子组件（带插槽）
import HeaderTop from "../../components/HeaderTop/HeaderTop";
// 引入购物车数量加减子组件
import CartControl from "../../components/CartControl.vue";
// 引入商品信息子组件
import ProductItem from "../../components/ProductItem.vue";
// 引入购物车子组件
import ShopCart from "../../components/ShopCart";

export default {
  name: "ProductList",
  components: { HeaderTop, CartControl, ProductItem, ShopCart },
  data() {
    return {
      value: "", //搜索框内容
      categoryName: [], //商品类别
      categoryId: "1", // 商品类别 Id,设置初始值为1
      shopList: {}, // 商品列表
      addressStore: {} //店铺地址
      // showPopup: false, //商品详情框默认不弹出
    };
  },
  computed: {
    ...mapState([
      "addressInfoNow",
      "ProductData",
      "selectedFood",
      "isLogin",
      "addressTitle"
    ]) //获取店铺名称
  },
  methods: {
    // 优先定位
    isPosition() {
      // 是否登录
      if (this.isLogin == null) {
        MessageBox.alert("请先登录").then(() => {
          this.$router.push("/login");
        });
      } else {
        // 是否获取定位
        if (this.addressInfoNow.title == "暂时无法获取定位") {
          Dialog.alert({
            title: "提示",
            message: "请先选择当前地址"
          }).then(() => {
            this.$router.push("/store");
          });
        }
      }
    },
    // 封装一个请求 商品分类 数据的函数
    getShopCategory() {
      this.axios.get("/api/shop/getShopCategory").then(result => {
        // console.log(result.data.data);
        this.categoryName = result.data.data;
      });
    },

    // 封装一个 根据商品分类的scId 来请求其对应分类的商品 数据的函数
    getShopByCategory() {
      this.axios
        .get("/api/shop/getShopByCategory", {
          params: { categoryId: this.categoryId }
        })
        .then(result => {
          // console.log(result.data.data);
          this.shopList = result.data.data;
          // 将请求到的数据存储到 localStorage 中
          // localStorage.setItem("shopListData", this.shopList);
        });
      // 滚动到顶部
      document.documentElement.scrollTop = 0;
    },

    // 点击商品分类时，查询对应的商品信息
    changeCategory(categoryId) {
      this.categoryId = categoryId;
      // console.log(categoryId);
      // console.log(this.categoryId);
      this.getShopByCategory();
    }

    // 点击后商品详情框弹出
    /* showPopup() {
      this.showPopup = true;
    }, */
  },
  created() {},
  mounted() {
    // 测试
    if (typeof this.addressInfoNow == "string") {
      this.addressStore = JSON.parse(this.addressInfoNow);
      console.log(this.addressStore);
    }
    // console.log(typeof this.addressInfoNow);

    // 调用 vuex 中的 action 中的方法，得到总的商品列表 ProductData
    // this.$store.dispatch("getProductData");
    // setTimeout(() => {
    //   console.log(this.$store.state.ProductData);
    // }, 5000);

    // 页面挂载时，请求商品类别
    this.getShopCategory();
    // 页面挂载时，请求商品类别 scId 为 1 的商品信息
    this.getShopByCategory();
  },
  // 定义过滤器
  filters: {
    shopIntrFilter(oldVal) {
      if (oldVal.length > 22) {
        return oldVal.slice(0, 22) + "...";
      }
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}

/* 搜索框部分 */
.search-wrap {
  /* position: relative;*/
  position: fixed;
  top: 40px;
  z-index: 110;
  width: 100%;
  height: 40px;
  /* border: 2px solid #ff8c00; */
  border-bottom: 1px solid #d6d6d6;
  background-color: #fff;
  /* padding: 2px 5px; */
  /* border-radius: 15px; */
}
.search-wrap > .search {
  position: absolute;
  border: 2px solid #ff8c00;
  border-radius: 15px;
  width: 98%;
  height: 30px;
  margin: 3px 5px;
  overflow: hidden;
}
.search-wrap .search-icon {
  position: absolute;
  height: 20px;
  width: 20px;
  background: url(/xrzp/imgs/search.png) no-repeat;
  background-size: 20px 20px;
  top: 3px;
  left: 5px;
}
.search-icon::after {
  content: "";
  display: block;
  height: 16px;
  border: 1px solid #cccccc;
  position: absolute;
  right: -8px;
  top: 2px;
}
.search-wrap input {
  height: 26px;
  width: 100%;
  margin-left: 42px;
  border: none;
}
.search-wrap input::placeholder {
  color: #a9a9a9;
  font-size: 14px;
}

/* 商品内容部分 */
/* 左侧导航部分 */
.content {
  position: relative;
  top: 75px;
  /* background-color: greenyellow; */
  margin-top: 5px;
}
.content .left {
  position: fixed;
  overflow-y: scroll;
  /* float: left; */
  left: 0;
  top: 80px;
  bottom: 55px;
  width: 25%;
  /* background-color: #ffd700; */
}
/* 商品分类 */
.content .left ul li {
  display: block;
  width: 100%;
  height: 80px;
  /* border-bottom: 1px solid #d6d6d6; */
  text-align: center;
  /* line-height: 35px; */
}
/* 点击时商品类别时的样式变化 */
.click {
  border-left: 3px solid #ff8c00;
}
/* 商品类别图标 */
.content .left ul li img {
  height: 40px;
  margin-top: 5px;
}

/* 右侧商品部分 */
.content .right {
  position: fixed;
  z-index: -1;
  right: 0;
  overflow-y: scroll;
  top: 80px;
  bottom: 55px;
}
.content .right ul li {
  float: right;
  position: relative;
  /* background-color: greenyellow; */
  padding: 5px;
  height: 90px;
  border-left: 1px solid #e6e6e6;
  /* border-bottom: 1px solid #e6e6e6; */
  width: 75%;
  margin: 2px 0;
}
.content .right img {
  height: 80px;
  width: 80px;
  background-color: #fff;
}
.content .right .text {
  height: 80px;
  position: absolute;
  left: 95px;
  top: 5px;
  font-family: "Avenir, Helvetica, Arial, sans-serif";
}
.content .right .text h5 {
  /* font-weight: 700; */
  font-size: 17px;
}
.content .right .text h6 {
  font-size: 12px;
  margin-top: 5px;
  color: #868686;
}
.content .right .text span {
  font-size: 10px;
  color: #ff4d4d;
  font-family: "Microsoft YaHei";
  position: absolute;
  bottom: 5px;
}
.content .right .add {
  position: absolute;
  right: 12px;
  bottom: 10px;
}
</style>